package com.yuntongxun.ecdemo.voicechat.ui.activity;

import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

import com.diegocarloslima.byakugallery.lib.GalleryViewPager;
import com.diegocarloslima.byakugallery.lib.TouchImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.yuntongxun.ecdemo.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.OnClick;


/**
 * 浏览图片
 */
public class BrowserImageActivity extends BaseChatActivity
{
	public static final String INTENT_URL_KEY = "url";
	public static final String INTENT_POSITION_KEY = "position";
	public static final String INTENT_TITLE_KEY = "title";
	public static final String INTENT_SELECTED_IMAGES_KEY = "selected_images";// 已经选择过的图片

	private GalleryViewPager pager;

	private List<String> imageUrls = new ArrayList<String>();
	private int initPosition = 0;

	static int RESULT_CODE = 120;

	@OnClick(R.id.toolbar_back_btn)
	public void onclcikBack(){
		onBackPressed();
	}
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//        int id = item.getItemId();
//
//        //noinspection SimplifiableIfStatement
//        if (id == android.R.id.home) {
//            onBackPressed();
//        }
//
//        return super.onOptionsItemSelected(item);
//    }

	@Override
	protected boolean isBindEventBusHere() {
		return false;
	}

	@Override
	protected int getContentViewLayoutID() {
		return R.layout.activity_browser_images;
	}

	@Override
	protected void initViewsAndEvents() {
		init();

		initViews();

		initListeners();

		setIsSelectedStatus(initPosition);
	}

	private void init()
	{
		if (getIntent().hasExtra(INTENT_URL_KEY))
		{
			imageUrls = getIntent().getStringArrayListExtra(INTENT_URL_KEY);

		}
		if (getIntent().hasExtra(INTENT_POSITION_KEY))
		{
			initPosition = getIntent().getIntExtra(INTENT_POSITION_KEY, 0);
		}
		if (getIntent().hasExtra(INTENT_TITLE_KEY))
		{
			setTitle(getIntent().getStringExtra(INTENT_TITLE_KEY));
		}

	}

	private void initViews()
	{
//        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setTitle((initPosition + 1) + "/" + imageUrls.size());

		pager = (GalleryViewPager) findViewById(R.id.view_pager);
		pager.setOffscreenPageLimit(0);
		pager.setAdapter(new ImagePagerAdapter(imageUrls));
		pager.setCurrentItem(initPosition);

	}

	private void initListeners()
	{
		pager.setOnPageChangeListener(new OnPageChangeListener()
		{
			@Override
			public void onPageSelected(int position)
			{
				initPosition = position;
                setTitle((position + 1) + "/" + imageUrls.size());
				
				setIsSelectedStatus(position);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2)
			{
			}

			@Override
			public void onPageScrollStateChanged(int arg0)
			{

			}
		});

	}

	/**
	 * @param position
	 */
	private void setIsSelectedStatus(int position)
	{

	}

	private class ImagePagerAdapter extends PagerAdapter
	{
		private List<String> images;
		private LayoutInflater inflater;

		ImagePagerAdapter(List<String> images)
		{
			this.images = images;
			inflater = getLayoutInflater();
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object)
		{
			container.removeView((View) object);
		}

		@Override
		public void finishUpdate(View container)
		{
		}

		@Override
		public int getCount()
		{
			return images.size();
		}

		@Override
		public Object instantiateItem(ViewGroup view, int position)
		{
			View imageLayout = inflater.inflate(R.layout.browser_images_item_pager,
					view, false);

			String sourceUrl = images.get(position);
			
			if(!sourceUrl.startsWith("http://"))
				sourceUrl = "file://" + sourceUrl;
//			final String sourceUrl = images.get(position);

			TouchImageView imageView2 = (TouchImageView) imageLayout
					.findViewById(R.id.image2);
            imageView2.setMaxScale(5);

            imageView2.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					onBackPressed();
				}
			});

			Log.e("e",sourceUrl);

//			Picasso.with(BrowserLocalImageActivity.this)
//					.load(new File(sourceUrl)).resize(960, 960).centerInside().into(imageView2);
			
			ImageLoader.getInstance().displayImage(sourceUrl, imageView2,
					DisplayImageOptions.createSimple(), null);
			
			view.addView(imageLayout, LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT);
			return imageLayout;
		}

		@Override
		public boolean isViewFromObject(View view, Object object)
		{
			return view.equals(object);
		}

		@Override
		public void destroyItem(View container, int position, Object object)
		{
			super.destroyItem(container, position, object);
		}

		@Override
		public void restoreState(Parcelable state, ClassLoader loader)
		{
		}

		@Override
		public Parcelable saveState()
		{
			return null;
		}

		@Override
		public void startUpdate(View container)
		{
		}
	}
}
